<extend name="Public:index" />
<block name="mycss">
	<link href="__PUBLIC__/assets/plugins/custombox/custombox.min.css" rel="stylesheet" type="text/css">
	<link href="__PUBLIC__/assets/plugins/select2/select2.min.css" rel="stylesheet" type="text/css">
	<style>
	.pro_state{
		position: absolute;
		border:1px solid #f93b7a;
		border-radius: 6px;
		padding: 10px 15px;
		color:  #f93b7a;
		transform: rotate(-45deg);
		top:115px;
		left: 5px;
	}
	.avatar_box{
		min-height: 32px;
	}
	.select2-container{width:100% !important;}
	</style>
</block>
<block name="main-content">
	<iframe name="ajaxifr" style="display:none;"></iframe>
	<div class="container-fluid">
		<php>
			$classes = array('bg-primary','bg-pink','bg-success','bg-dark');
			$x = 0;
		</php>
		<div class="row">
			<volist name="list" id="vo" mod="4">
				<php>$x = $key - (int)($key / 4) * 4;</php>
				<div class="col-lg-3">

					<div class="card profile-card">

						<div class="card-body {$vo.color} p-0">
							<div class="media p-3  align-items-center">

								<div class="media-body ml-3 align-self-center">
									<a data-url="{:U('detail',array('id'=>$vo['id']))}" href="{:U('detail',array('id'=>$vo['id']))}">
										<h4 class="mb-1 text-white">{$vo.project_name}</h4>
										<p class="mb-0 font-12 text-white">{$vo.project_addr}</p>
									</a>
								</div>

								<div class="dropdown d-inline-block float-right">
									<a class="nav-link dropdown-toggle mr-n2 mt-n2" id="drop6" data-toggle="dropdown" href="#" role="button"
									 aria-haspopup="false" aria-expanded="false">
										<i class="fas fa-ellipsis-v text-white"></i>
									</a>

									<div class="dropdown-menu dropdown-menu-right" aria-labelledby="drop6">
										<a class="dropdown-item text-success" href="#" onclick="copyProject({$vo.id});">复制</a>
									<if condition="($vo.user_id eq $userid) OR ($vo.demo neq 1)">
										<a class="dropdown-item text-info" href="#" data-toggle="modal" data-target="#editModal" data-backdrop="static"
										 data-id="{$vo.id}">编辑</a>
										 <eq name="vo.user_id" value="$userid">
										<a class="dropdown-item text-danger" href="#" onclick="delProject({$vo.id});">删除</a>
										</eq>
											<!-- 协同项目 没有删除按钮 -->
										<if condition="($vo.user_id neq $userid) AND ($_SESSION['roleid'] neq 1)">
										<a class="dropdown-item text-warning" href="#" onclick="cancelHelp({$vo.id});">取消协同</a>
										</if>
									</if>
									</div>
								</div>
								<!--end dropdown-->
							</div>
						</div>

						<!--end card-body-->
						<div class="card-body pb-0">
							<h6 class="text-center">项目编号：{$vo.project_code}</h6>
							<p class="font-14 text-muted text-center">{$vo['intro']?:'&nbsp;'}</p>
							<neq name="Think.session.roleid" value="1">
								<neq name="vo.user_id" value="$userid">
									<neq name="vo.demo" value="1">
									<div class="pro_state">受邀项目</div>
									<else/>
									<div class="pro_state">示例项目</div>
									</neq>
								</neq>
							<else />
								<neq name="vo.user_id" value="$userid">
									<div class="pro_state">{$vo.username}</div>
								</neq>
							</neq>
							<ul class="list-inline list-unstyled profile-socials text-center mb-0 avatar_box">
								<php> 
									foreach($headpics[$vo['id']] as $headpic):
										$pic = $headpic['image_url'] ? '__ROOT__'.$headpic['image_url'] : '__PUBLIC__/assets/images/users/user-0.jpg';
								</php>
								
								<li class="list-inline-item">
									<a href="#" class=""> <img src="{$pic}" data-toggle="tooltip" data-placement="top" title="{$headpic.username}" class="thumb-xs rounded-circle"></a>
								</li>
								<php>endforeach;</php>
							</ul>
						</div>
						<!--end card-body-->
						<div class="card-body socials-data pb-0 px-0">
							<div class="row text-center border-top m-0">
								<div class="col-4 item-box border-right py-3">
									<h3 class="mt-0 mb-1">{$floor_nums[$vo['id']]?:0}</h3>
									<span class="font-14 text-muted">楼层</span>
									<div class="item-mask" data-toggle="modal" data-target="#FloorModal" data-id="{$vo.id}"
									data-backdrop="static">
										<div class="item-caption">
										
											<p class="text-light"></p>
										</div>
									</div>
								</div>
								<!--end col-->
								<div class="col-4 border-right py-3">
									<h3 class="mt-0 mb-1">{$device_nums[$vo['id']]?:0}</h3>
									<span class="font-14 text-muted">CPN</span>
								</div>
								<!--end col-->
								<div class="col-4 py-3">
									<h3 class="mt-0 mb-1">{$sensor_nums[$vo['id']]?:0}</h3>
									<span class="font-14 text-muted">末端设备</span>
								</div>
								<!--end col-->
							</div>
							<!--end row-->
						</div>
						<!--end card-body-->
					</div>
					<!--end card-->
				</div>
				<!--end col-->
				<eq name="mod" value="3">
		</div>
		<div class="row">
			</eq>
			</volist>
		</div>
		<!--end row-->
		<div class="row">
			<div class="col-sm-12">
				<div class="pb-4">
					<button class="btn btn-info px-4 align-self-center waves-effect create-btn" data-toggle="modal" data-target="#editModal"
					data-backdrop="static">
						<i class="mdi  mdi-plus"></i>
						创建项目</button>

				</div>
				<!--end page title box-->
			</div>
			<!--end col-->

		</div>
		<!--end row-->

	</div>
	<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModal" aria-hidden="true">
		<div class="modal-dialog modal-lg" role="document">
			<form class="" method="post" action="{:U('editProject')}" target="ajaxifr">
				<div class="modal-content">
				</div>
			</form>
		</div>
	</div>
	<div class="modal fade" id="FloorModal" tabindex="1" role="dialog" aria-labelledby="FloorModal" aria-hidden="true">
		<div class="modal-dialog modal-lg" role="document">
			<form class="" method="post" action="{:U('editFloor')}" target="ajaxifr">
				<div class="modal-content">

				</div>
			</form>
		</div>
	</div>
</block>
<block name="myscript">
	<script src="__PUBLIC__/assets/plugins/custombox/custombox.min.js"></script>
	<script src="__PUBLIC__/assets/plugins/custombox/custombox.legacy.min.js"></script>
	<script src="__PUBLIC__/assets/plugins/select2/select2.min.js"></script>
	<script src="__PUBLIC__/assets/plugins/repeater/jquery.repeater.min.js"></script>
	<script src="__PUBLIC__/web/js/jquery.project.js"></script>
	<script>
		$(function(){
			$('[data-toggle="tooltip"]').tooltip();
		});

		$('#editModal').on('show.bs.modal', function (e) {
			var obj = $(e.relatedTarget);
			var id = obj.data('id');
			var _this = this;
			var url = "{:U('editProject')}";
			$.ajax({
				type: 'GET',
				url: url,
				data: {id: id}
			}).then(function (data) {
				$(_this).find('.modal-content').html(data);

				/*
				$('#select2').select2({
					placeholder: '选择模版包...',
					tags: true,
					ajax: {
						url: "{:U('getModule','','')}/id/" + id,
						dataType: 'json',
						delay: 250  // wait 250 milliseconds before triggering the request
					}
				});
				//清除所有当前选项
				//$('.select2').val(null).trigger('change');

				// Fetch the preselected item, and add to the control
				var selectObj = $('#select2');
				$.ajax({
					type: 'GET',
					url: '{:U('getSelModule','','')}/id/' + id
				}).then(function (data) {
					// create the option and append to Select2
					for (var rs of data.results) {
						var option = new Option(rs.text, rs.id, true, true);
						selectObj.append(option).trigger('change');
					}
					// manually trigger the `select2:select` event
					selectObj.trigger({
						type: 'select2:select',
						params: {
							data: data
						}
					});
				});
				*/
			});
		})

		$('#FloorModal').on('show.bs.modal', function (e) {
			var obj = $(e.relatedTarget);
			var id = obj.data('id');
			var url = "{:U('editFloor')}";
			var _this = this;
			$.get(url, {project_id:id}, function(res){
				$(_this).find('.modal-content').html(res);
			});
		});

		function delProject(id) {
			var url = "{:U('delProject')}";
			if (confirm("确认删除此项目吗？")) {
				$.get(url, { id: id }, function (res) {
					if (res == '1') {
						window.location.reload();
					} else {
						notify("error","提示","不能删除此项目！");
					}
				});
			}
		}

		function copyProject(id) {
			var url = "{:U('copyProject')}";
			$.get(url, { id: id }, function (res) {
				if (res == '1') {
					window.location.reload();
				}
			});
		}

		function cancelHelp(id){
			var url = "{:U('cancelHelp')}";
			$.get(url, {id: id}, function(res){
				if(res == '1'){
					window.location.reload();
				}
			});
		}

	</script>
</block>